package com.cn.util;

import cn.dev33.satoken.stp.StpUtil;
import com.cn.error.ErrorConstantUtil;
import com.cn.exception.MyException;
import com.cn.global.AbstractController;
import com.cn.user.UserDO;

/**
 * description: BaseController <br>
 * date: 22.2.21 10:28 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
public class BaseController extends AbstractController {

    public final static String sessionKey = "user";

    public void checkLogin() {
        try {
            StpUtil.checkLogin();
        } catch (Exception e) {
            throw new MyException(ErrorConstantUtil.LOGIN_EXPIRE, 201);
        }
    }

    /**
     * 获取登录会话
     *
     * @return
     */
    public UserDO getToken() {
        UserDO user = null;
        try {
            user = (UserDO) StpUtil.getSession().get(sessionKey);
        } catch (Exception e) {
            throw new MyException(ErrorConstantUtil.LOGIN_EXPIRE, 201);
        }
        return user;
    }

    public UserDO getUser() {
        return getToken();
    }


}
